<?php
/**
 * IpHelper.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/11/12- 11:59 上午
 */

namespace common\helpers;
use common\components\Ip2Region\Ip2Region;
use Yii;

class IpHelper
{


    /**
     * 通过IP 获取对应的省市区信息
     * @param string $ip
     * @return array
     */
    public static function getRegion(string $ip)
    {

        $ip2RegionObj = new Ip2Region();
        $data = $ip2RegionObj->binarySearch($ip)['region'];
        $region = [];
        list($region['country'],$region['area'],$region['province'],
            $region['city'],$region['isp']) = explode('|',$data);
        if($region['city'] == '内网IP') $region['city'] = 0;
        if($region['isp'] == '内网IP') $region['isp'] = 0;
        return $region;
    }
}
